package com.seleniti.enpareja.android.ui.widget;

import org.ioc.commons.impl.android.ui.LinearLayoutDisplay;
import org.ioc.commons.impl.android.ui.SavedState;
import org.ioc.commons.ui.IsWidget;

import android.content.Context;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.HasFrameLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.seleniti.enpareja.android.R;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Action;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Event;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Operation;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Presenter;

public class SliderPanelWidget extends LinearLayoutDisplay<Event, Operation, Action, Presenter> implements SliderPanel,
		HasFrameLayout {

	private ImageView logo;
	private BoardType boardType;
	private FrameLayout container;
	private IsWidget widget;
	private LayoutInflater inflater;

	public SliderPanelWidget(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.slider_panel, this, true);
		this.logo = (ImageView) findViewById(R.id.shell_img_logo);
		this.container = (FrameLayout) findViewById(R.id.shell_container);
	}

	public SliderPanelWidget(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SliderPanelWidget(Context context) {
		this(context, null);
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		if (this.boardType != null) {
			SavedState ss = new SavedState(superState);

			ss.setStateToSave(this.boardType.ordinal());

			return ss;
		} else {
			return superState;
		}
	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		// begin boilerplate code so parent classes can restore state
		if (!(state instanceof SavedState)) {
			super.onRestoreInstanceState(state);
			return;
		}

		SavedState ss = (SavedState) state;
		super.onRestoreInstanceState(ss.getSuperState());
		// end

		BoardType boardType = BoardType.values()[ss.getStateToSave()];
		this.setBoardType(boardType);
	}

	@Override
	public BoardType getBoardType() {
		return this.boardType;
	}

	@Override
	public void setBoardType(BoardType boardType) {
		this.boardType = boardType;
		if (boardType != null) {
			switch (boardType) {
			case WITHOUT_LOGO:
				this.logo.setVisibility(GONE);
				break;
			case WITH_LOGO:
				this.logo.setVisibility(VISIBLE);
				break;
			}
		}
	}

	@Override
	public void setSingleWidget(IsWidget widget) {
//		this.container.removeAllViews();
		this.widget = widget;
//		if (widget != null) {
//			this.addView((View) widget.asWidget());
//		}
	}

	@Override
	public IsWidget getSingleWidget() {
		return this.widget;
	}

	@Override
	public FrameLayout getFrameLayout() {
		return this.container;
	}

}
